<template>
  <div class="edit">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/Index/questionMange/knowledge_spot' }">网球规则</el-breadcrumb-item>
      <el-breadcrumb-item
        :to="{ path: '/index/questionMange/tennisRule?id='+K_points_list_id+'&name='+name+'' }"
      >{{name}}</el-breadcrumb-item>
      <el-breadcrumb-item>{{type}}</el-breadcrumb-item>
    </el-breadcrumb>
    <el-form label-position="left" ref="form" :model="form" label-width="80px" class="form">
      <div style="display: flex;">
        <el-form-item label="题库类型:">
          <el-select
            v-model="form.rule"
            style="width: 185px;margin-right: 20px;"
            placeholder="网球规则:"
            @change="ruleChange"
          >
            <el-option
              :label="item.cate_name"
              :value="item.id"
              v-for="(item, index) in getLibTypeArr"
              :key="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="知识点名称:" label-width="100px">
          <el-select
            v-model="form.knowledgeT"
            style="width: 185px;"
            placeholder="知识点名称:"
            @change="knowledgeChange"
          >
            <el-option
              :label="item.cate_name"
              :value="item.id"
              v-for="(item, index) in knowledgeArr"
              :key="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
      </div>
      <el-form-item label="标题:">
        <el-input style="width: 490px;" placeholder="关于如何正确发球" v-model="form.title"></el-input>
      </el-form-item>
    </el-form>
    <Uediter :value="ueditor.value" :config="ueditor.config" ref="ue"></Uediter>
    <div style="text-align: center;margin-top: 40px;">
      <el-button type="primary" @click="returnContent">提交</el-button>
    </div>
  </div>
</template>
<script>
import Uediter from "@/components/UE/UE.vue";
export default {
  data() {
    return {
      id: this.$route.query.id || "",
      type: this.$route.query.type,
      K_points_list_id: this.$route.query.K_points_list_id,
      name: this.$route.query.name,
      form: {
        rule: "",
        knowledgeT: "",
        title: ""
      },
      getLibTypeArr: [],
      knowledgeArr: [],
      content: "",
      ueditor: {
        value: "",
        config: {
          initialFrameWidth: 800,
          initialFrameHeight: 320
        }
      }
    };
  },
  created() {
    this.getLibType();
  },
  mounted() {
    this.edit_point_info().then(c_cate_id => {
      this.ruleChange(c_cate_id)
    });
  },
  methods: {
    edit_point_info() {
      return new Promise((reslove, reject) => {
        this.$axios
          .post(
            "/Qlib/edit_point_info",
            {
              id: this.id
            },
            {
              headers: {
                token: window.sessionStorage.getItem("token")
              }
            }
          )
          .then(res => {
            const { code, data, msg } = res.data;
            if (code == 2000) {
              this.ueditor.value = data.k_content;
              var ue = UE.getEditor("editor");
              ue.setContent(this.ueditor.value);
              this.form.knowledgeT = data.b_cate_id;
              this.form.rule = data.c_cate_id;
              this.form.title = data.k_title;
              reslove(data.c_cate_id);
            } else {
              reject();
            }
          })
          .catch(error => {});
      });
    },
    getLibType() {
      this.$axios
        .post(
          "/Qlib/getLibType",
          {},
          {
            headers: {
              token: window.sessionStorage.getItem("token")
            }
          }
        )
        .then(res => {
          const { code, data, msg } = res.data;
          if (code == 2000) {
            this.getLibTypeArr = data;
          }
        })
        .catch(error => {});
    },
    returnContent() {
      this.content = this.$refs.ue.getUEContent();
      this.$axios
        .post(
          "/Qlib/savePointContent",
          {
            id: this.id,
            cate_id: this.form.knowledgeT,
            k_title: this.form.title,
            k_content: this.content
          },
          {
            headers: {
              token: window.sessionStorage.getItem("token")
            }
          }
        )
        .then(res => {
          const { code, data, msg } = res.data;
          if (code == 2000) {
            this.$message({
              type: "success",
              message: msg
            });
          } else {
            this.$message({
              type: "info",
              message: msg
            });
          }
        })
        .catch(error => {});
    },
    ruleChange(val) {
      return new Promise((reslove, reject) => {
        this.$axios
          .post(
            "/Qlib/select_cate_name",
            {
              id: val
            },
            {
              headers: {
                token: window.sessionStorage.getItem("token")
              }
            }
          )
          .then(res => {
            const { code, data, msg } = res.data;
            if (code == 2000) {
              this.knowledgeT = val;
              this.knowledgeArr = data;
              reslove(data);
            } else {
              this.$message({
                type: "info",
                message: msg
              });
            }
          })
          .catch(error => {});
      });
    },
    knowledgeChange() {}
  },
  components: {
    Uediter
  }
};
</script>

<style lang="less" scoped>
.edit {
  margin: 20px 50px;

  .form {
    margin-top: 20px;
  }
}
</style>
